/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.psdk.util;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import com.qihoo.psdk.app.QStatActivity;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class d implements View.OnTouchListener {
    private Context a = null;
    private com.qihoo.psdk.local.g b = null;
    private String c = "";
    private WindowManager d;
    private Point e = new Point();
    private int f = 0;
    private View g;
    private WindowManager.LayoutParams h;
    private Point i = null;
    private int j = 11;
    private long k = 0L;
    private long l = 0L;
    private float m = 1.0F;
    private long n = 0L;
    private boolean o = false;
    private boolean p = true;
    private long q = 0L;
    private g r = new g(null);
    private g s = new g(null);
    private Timer t = null;
    private TimerTask u = null;
    private Handler v = new e(this);

    public static void a(Context paramContext, com.qihoo.psdk.local.g paramg, String paramString) {
        try {
            new d(paramContext, paramg, paramString).d(paramContext, paramg, paramString);
        } catch (Exception localException) {
            h.a("QFloatMgr", localException);
        } catch (Error localError) {
            h.a("QFloatMgr", localError);
        }
    }

    public d(Context paramContext, com.qihoo.psdk.local.g paramg, String paramString) {
        this.a = paramContext;
        this.b = paramg;
        this.c = paramString;
    }

    private void b(Context paramContext, com.qihoo.psdk.local.g paramg, String paramString) {
        this.f = (c.a(paramContext, 10.0F) / 2);
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        localDisplayMetrics = paramContext.getResources().getDisplayMetrics();
        if (this.e == null)
            this.e = new Point();
        this.e.x = localDisplayMetrics.widthPixels;
        this.e.y = localDisplayMetrics.heightPixels;
        this.d = ((WindowManager) paramContext.getApplicationContext().getSystemService("window"));
        this.h = b();
        int i1 = localDisplayMetrics.widthPixels;
        if (localDisplayMetrics.widthPixels > localDisplayMetrics.heightPixels)
            i1 = localDisplayMetrics.heightPixels;
        if (paramg.j < 0.0D) {
            this.h.width = (i1 / 2);
            this.h.height = (i1 / 6);
        }
        else {
            this.h.width = (int) (i1 * paramg.j);
            this.h.height = (int) (i1 * paramg.k);
        }
        int i2 = 0 - this.f;
        if (this.i == null) {
            this.h.x = i2;
            this.h.y = i2;
        }
        else {
            this.h.x = this.i.x;
            this.h.y = this.i.y;
        }
        if ((this.h.x < i2) || (this.h.x > this.e.x))
            this.h.x = i2;
        if ((this.h.y < i2) || (this.h.y > this.e.y))
            this.h.y = i2;
        a();
        c(paramContext, paramg, paramString);
    }

    private void a() {
        if (this.u == null)
            this.u = new f(this);
        if (this.t != null)
            return;
        this.t = new Timer();
        this.t.schedule(this.u, 1000L, 30L);
    }

    private WindowManager.LayoutParams b() {
        WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(-1, -1, 2003, 552, -2);
        localLayoutParams.gravity = 51;
        localLayoutParams.x = 0;
        localLayoutParams.y = 0;
        localLayoutParams.format = 1;
        return localLayoutParams;
    }

    private void c(Context paramContext, com.qihoo.psdk.local.g paramg, String paramString) {
        this.g = new QFloatLayout(paramContext, paramg, paramString);
        e();
    }

    private void d(Context paramContext, com.qihoo.psdk.local.g paramg, String paramString) {
        try {
            if (this.g != null)
                d();
            b(paramContext, paramg, paramString);
            if (this.h.x <= this.e.x / 2)
                this.j = 11;
            else
                this.j = 12;
            this.d.addView(this.g, this.h);
            this.g.setOnTouchListener(this);
        } catch (Exception localException) {
            h.a("QFloatMgr", localException);
        } catch (Error localError) {
            h.a("QFloatMgr", localError);
        }
    }

    private void c() {
        if (this.h == null)
            return;
        if (this.i == null)
            this.i = new Point();
        this.i.x = this.h.x;
        this.i.y = this.h.y;
    }

    private void d() {
        try {
            c();
            if (this.t != null)
                this.t.cancel();
            if (this.g != null)
                this.d.removeView(this.g);
            this.g = null;
            this.h = null;
            this.d = null;
            this.e = null;
            this.t = null;
            this.u = null;
            this.o = false;
            this.p = true;
            this.k = 0L;
            this.l = 0L;
            this.n = 0L;
        } catch (Exception localException) {
            h.a("QFloatMgr", localException);
        } catch (Error localError) {
            h.a("QFloatMgr", localError);
        }
    }

    private void a(int paramInt1, int paramInt2) {
        if ((this.g == null) || (this.e == null))
            return;
        if ((this.g.getHeight() <= 0) || (this.g.getWidth() <= 0))
            return;
        int i1 = this.g.getWidth();
        int i2 = this.e.x;
        int i3 = i2 - i1;
        this.h.x = (paramInt1 - (this.g.getWidth() / 2));
        this.h.y = (paramInt2 - (int) (this.g.getHeight() * 1.2F));
        if (this.h.x > i3)
            this.h.x = i3;
        else if (this.h.x < 0)
            this.h.x = 0;
        if (this.h.y > this.e.y)
            this.h.y = this.e.y;
        else if (this.h.y < 0 - this.f)
            this.h.y = (0 - this.f);
        this.d.updateViewLayout(this.g, this.h);
    }

    public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
        int i1 = paramMotionEvent.getAction();
        switch (i1 & 0xFF) {
        case 0:
            this.q = System.currentTimeMillis();
            this.r.a = paramMotionEvent.getRawX();
            this.r.b = paramMotionEvent.getRawY();
            this.s.a = paramMotionEvent.getRawX();
            this.s.b = paramMotionEvent.getRawY();
            break;
        case 1:
        case 3:
            this.s.a = paramMotionEvent.getRawX();
            this.s.b = paramMotionEvent.getRawY();
            h();
            this.q = 0L;
            this.r.a = (this.r.b = this.s.a = this.s.b = 0.0F);
            break;
        case 2:
            this.s.a = paramMotionEvent.getRawX();
            this.s.b = paramMotionEvent.getRawY();
            if (a(this.q, this.r, this.s))
                break label204;
            g();
        }
        label204: return false;
    }

    public static boolean a(long paramLong, g paramg1, g paramg2) {
        long l1 = System.currentTimeMillis();
        long l2 = l1 - paramLong;
        float f1 = Math.abs(paramg2.a - paramg1.a);
        float f2 = Math.abs(paramg2.b - paramg1.b);
        return ((l2 <= 200L) && (f1 <= 10.0F) && (f2 <= 10.0F));
    }

    private void e() {}

    private void f() {
        this.j = 40;
    }

    private void g() {
        f();
        a((int) this.s.a, (int) this.s.b);
    }

    private void h() {
        e();
        if (a(this.q, this.r, this.s)) {
            j();
            return;
        }
        if (this.j != 40)
            return;
        i();
    }

    private void i() {
        if ((this.h == null) || (this.g == null) || (this.e == null) || (this.j != 40))
            return;
        int i1 = this.g.getWidth();
        int i2 = this.e.x;
        int i3 = 0 - this.f;
        int i4 = i2 - i1 + this.f;
        if (this.h.x == i3) {
            this.j = 11;
            return;
        }
        if (this.h.x == i4) {
            this.j = 12;
            return;
        }
        int i5 = this.h.x;
        int i6 = i2 - i5 - i1;
        this.j = 11;
        int i7 = i5;
        if (i7 <= i6)
            return;
        this.j = 12;
        i7 = i6;
    }

    private void j() {
        try {
            h.a("QFloatMgr", "doIconClick");
            d();
            Intent localIntent = new Intent(this.a, QStatActivity.class);
            String str = a.a(this.c);
            int i1 = new Random().nextInt();
            if (i1 < 0)
                i1 = -i1;
            localIntent.putExtra("url", this.b.p);
            localIntent.putExtra("activity", this.b.q);
            localIntent.putExtra("application", this.b.r);
            localIntent.putExtra("msgId", str);
            if (!(TextUtils.isEmpty(this.b.s)))
                localIntent.putExtra("custom", this.b.s);
            localIntent.putExtra("app", this.b.t);
            localIntent.putExtra("notifyId", i1);
            localIntent.addFlags(268435456);
            this.a.startActivity(localIntent);
        } catch (Exception localException) {
            h.a("QFloatMgr", localException);
        } catch (Error localError) {
            h.a("QFloatMgr", localError);
        }
    }

    private void k() {
        if ((this.j != 11) && (this.j != 12))
            return;
        if ((this.h == null) || (this.e == null) || (this.d == null) || (this.g == null))
            return;
        int i1 = this.g.getWidth();
        int i2 = this.e.x;
        int i3 = 0 - this.f;
        int i4 = i2 - i1 + this.f;
        if (this.h.x == i3)
            return;
        if (this.h.x == i4)
            return;
        switch (this.j) {
        case 11:
            this.h.x -= 20;
            if (this.h.x < i3)
                this.h.x = i3;
            break;
        case 12:
            this.h.x += 20;
            if (this.h.x > i4)
                this.h.x = i4;
        }
        this.d.updateViewLayout(this.g, this.h);
    }
}